package ru.spb.osll.myfit.client.widget;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.*;
import ru.spb.osll.myfit.common.PageTag;

public class TagWidget extends Composite {
  private FlowPanel m_widget;
  private PageTag m_tag;
  private TagWidgetEventHandler m_eventHandler;
  private boolean m_enabled = true;

  public TagWidget(PageTag tag, TagWidgetEventHandler eventHandler) {
    m_tag = tag;
    m_eventHandler = eventHandler;
    m_widget = new FlowPanel();
    initWidget(m_widget);

    m_widget.addStyleName(IStyleConstants.TAG_STYLE);

    Label closeLabel = new Label();
    closeLabel.addStyleName(IStyleConstants.TAG_CLOSE_LABEL_STYLE);
    setEnabled(false);
    closeLabel.addClickHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent event) {
        if (m_enabled) {
          ((Widget) event.getSource()).getParent().getParent().removeFromParent();
          if (m_eventHandler != null)
            m_eventHandler.onTagRemove(m_tag);
        }
      }
    });
    m_widget.add(closeLabel);

    Label m_titleLabel = new Label(tag.getString());
    m_titleLabel.addStyleName(IStyleConstants.TAG_TITLE_LABEL_STYLE);
    m_widget.add(m_titleLabel);
  }

  public void setEnabled(boolean enabled) {
    m_enabled = enabled;
    if (!enabled) {
      m_widget.removeStyleName(IStyleConstants.TAG_CLOSE_STYLE);
    }
    else {
      m_widget.addStyleName(IStyleConstants.TAG_CLOSE_STYLE);
    }
  }
}
